cmake_minimum_required( VERSION 2.8 )

project( fhe )

set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin )

include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )

set( ext_libs )

set( ext_dir ${CMAKE_CURRENT_SOURCE_DIR}/ext )
if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ext )
  execute_process( COMMAND mkdir ${CMAKE_CURRENT_SOURCE_DIR}/ext )
endif()

#boost
find_package( Boost COMPONENTS python chrono filesystem system REQUIRED )
include_directories( ${Boost_INCLUDE_DIRS} )
list( APPEND ext_libs ${Boost_LIBRARIES} )

#poco
list( APPEND ext_libs "PocoFoundation" )

#tinyxml
set( tinyxml_url "http://downloads.sourceforge.net/project/tinyxml/tinyxml/2.6.2/tinyxml_2_6_2.zip" )
set( tinyxml_file "tinyxml_2_6_2.zip" )
set( tinyxml_dir "tinyxml" )
if( NOT EXISTS ${ext_dir}/${tinyxml_file} )
  execute_process( COMMAND wget ${tinyxml_url} WORKING_DIRECTORY ${ext_dir} )
endif()
if( NOT EXISTS ${ext_dir}/${tinyxml_dir} )
  execute_process( COMMAND unzip ${tinyxml_file} WORKING_DIRECTORY ${ext_dir} )
endif()
file( GLOB_RECURSE tinyxml_srcs ${ext_dir}/${tinyxml_dir}/*.cpp )
add_definitions( -DTIXML_USE_STL )
add_library( tinyxml SHARED ${tinyxml_srcs} )
list( APPEND ext_libs tinyxml )
include_directories( ${ext_dir}/${tinyxml_dir} )

#gtest
set( gtest_url "https://googletest.googlecode.com/files/gtest-1.6.0.zip" )
set( gtest_file "gtest-1.6.0.zip" )
set( gtest_dir "gtest-1.6.0" )
if( NOT EXISTS ${ext_dir}/${gtest_file} )
  execute_process( COMMAND wget ${gtest_url} WORKING_DIRECTORY ${ext_dir} )
endif()
if( NOT EXISTS ${ext_dir}/${gtest_dir} )
  execute_process( COMMAND unzip ${gtest_file} WORKING_DIRECTORY ${ext_dir} )
endif()
if( NOT EXISTS ${ext_dir}/${gtest_dir}/libgtest.a )
  execute_process( COMMAND cmake . WORKING_DIRECTORY ${ext_dir}/${gtest_dir} )
  execute_process( COMMAND make WORKING_DIRECTORY ${ext_dir}/${gtest_dir} )
endif()
include_directories( ${ext_dir}/${gtest_dir}/include )
link_directories( ${ext_dir}/${gtest_dir} )
set( gtest_libs "gtest;gtest_main" )

#libfhe
file( GLOB_RECURSE fhesrcs fhe/*.cpp )
add_library( fhe SHARED ${fhesrcs} )
target_link_libraries( fhe ${ext_libs} )
set_target_properties( fhe PROPERTIES PREFIX "" )

#plugins
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/plugins )
file( GLOB plugindirs plugins/* )
foreach( plugindir ${plugindirs} )
  get_filename_component( pluginname ${plugindir} NAME )
  if ( NOT ${pluginname} STREQUAL ".svn" )
    if ( EXISTS ${plugindir}/CMakeLists.txt )
      include( ${plugindir}/CMakeLists.txt )
    else()
      file( GLOB_RECURSE pluginsrcs ${plugindir}/*.cpp )
      add_library( ${pluginname} SHARED ${pluginsrcs} )
      target_link_libraries( ${pluginname} fhe )
      set_target_properties( ${pluginname} PROPERTIES PREFIX "" )
    endif()
  endif()
endforeach()

#apps
file( GLOB appdirs apps/* )
foreach( appdir ${appdirs} )
  get_filename_component( appname ${appdir} NAME )
  if ( NOT ${appname} STREQUAL ".svn" )
    file( GLOB_RECURSE appsrcs ${appdir}/*.cpp )
    add_executable( ${appname} ${appsrcs} )
    target_link_libraries( ${appname} fhe )
  endif()
endforeach()

#tests
file( GLOB_RECURSE testsrcs tests/*.cpp )
add_executable( fhetests ${testsrcs} )
target_link_libraries( fhetests fhe ${gtest_libs} )
add_custom_target( runtest ALL COMMAND fhetests WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} )
